
var CKEDITOR_BASEPATH = '/ckeditor/';
var AUTOSAVE_ARTICLE = false;
var AUTOSAVE_HANDLE = null;

/**
 * AJAX 载入用户新日志页面
 *
 * @return 	void
 */
function bindUserNewArticle() {
	/**
	var uploadBoxObj = document.getElementById("user-body-box");
	uploadBoxObj.uploaded = function(uploadedResult, success) {
		if(!success) {
			addMmLightBoxMsg(uploadedResult, 1, 1);
		} else {
			addMmLightBoxMsg(uploadedResult, 1, 0, 0, function(){
				window.setTimeout(function(){
					removeMmLightBoxMsg();
				}, 1000);
			});
			$("#uploadFrom").get(0).reset();
		}
	};
	*/

	//ajax-upload-link
	$("#ajax-add-article-link").click(function(){
		$(".slider-item a").removeClass('selected-slider-item');
		$(this).addClass('selected-slider-item').blur();
		addMmLightBoxMsg('Wait a moment please, loading data ...', 0, 0, 1);
		var getUrl = $(this).attr('href');
		$.getScript('/ckeditor/ckeditor.js', function(){
			$.get(getUrl, function(dt){
				$("#user-body-box").height('auto').html(dt);
				CKEDITOR.replace('editor');
				if('loaded' == CKEDITOR.status) {
					removeMmLightBoxMsg();
				}

				if(AUTOSAVE_HANDLE) {
					window.clearInterval(AUTOSAVE_HANDLE);
				}

				window.setTimeout(function(){
					reactHeight();
				}, 5000);

				bindSaveEditArticle();

				AUTOSAVE_ARTICLE = true;

				$("#article-autosaved-count").text('0');

				$("#add-tag-botton").click(function(){
					filterAddTag();
				});

				$(".del-tag-link").click(function(){
					$(this).parent().remove();
					return false;
				});

				AUTOSAVE_HANDLE = window.setInterval("autosaveArticle()", 180000);
			});

		});
		return false;
	});
}

/**
 * 过滤重组 Tag 信息
 *
 * @return void
 */
function filterAddTag() {
	var tag = $("#add-tag-input").val();
	var addedTags = new Array();
	if(tag.length > 0) {
		var tags = tag.split(" ");
		for(var i in tags) {
			tags[i] = tags[i].replace(/\s/, "");
			if(tags[i].length > 0) {
				if(tags[i].length > 2) {
					var _tags = tags[i].split(",");
					for(var j in _tags) {
						if(_tags[j].length > 0) {
							addedTags.push(_tags[j]);
						}
					}
				} else {
					addedTags.push(tags[i]);
				}
			}
		}
	}

	$("#add-tag-input").val('');

	if(addedTags.length > 0) {
		var oldTags = new Array();
		$("#added-tags-box").find("span").each(function() {
			oldTags.push($(this).attr('name'));
		});
		$('#user-body-box').height('auto');

		for(var i in addedTags) {
			if( -1 == $.inArray(addedTags[i], oldTags)) {
				$("#old-tag-tpl").find("span").attr("name", addedTags[i]);
				$("#old-tag-tpl").find("span").find("input").attr("value", addedTags[i]);
				$("#old-tag-tpl").find("span").find("input").attr('checked', 'checked');
				$("#old-tag-tpl").find("span").find("label").text(addedTags[i]);

				$("#added-tags-box").append($("#old-tag-tpl").find("span").clone(true));

				$("#old-tag-tpl").find("span").attr("name", 'tpltag');
				$("#old-tag-tpl").find("span").find("input").attr("value", '');
				$("#old-tag-tpl").find("span").find("input").removeAttr('checked');
				$("#old-tag-tpl").find("span").find("label").text('');

				oldTags.push(addedTags[i]);
			}
		}
		reactHeight();
	}
}

/**
 * 自动保存日志内容
 *
 * @return void
 */
function autosaveArticle() {
	if(AUTOSAVE_ARTICLE) {
		var editContent = CKEDITOR.instances.editor.getData();
		var save_url = $("#submit-article").attr("name");
		if(save_url) {
			$(".article-status-editing").hide();
			$(".article-status-saving").show();
			$.post(save_url, {content:editContent}, function(dt){
				if(dt.result) {
					$(".article-status-editing label").html(dt.msg);
					var sc = parseInt($("#article-autosaved-count").text());
					sc += 1;
					$("#article-autosaved-count").text(sc);
				}
				$(".article-status-saving").hide();
				$(".article-status-editing").show();
			}, 'json');
		}
	}
}

/**
 * 保存日志信息
 *
 * @return void
 */
function bindSaveEditArticle() {
	$("#edit-article-form").submit(function(){
		var title = $("#edit-article-title-input").val();
		if(title.length < 2 || title.length > 200) {
			alert("Article title length can not allowed!");
			return false;
		}

		var content = CKEDITOR.instances.editor.getData();
		if(content.length < 20) {
			alert("The article content too little, write more ...");
			return false;
		}

		filterAddTag();
		var tags = new Array();
		$("#added-tags-box").find("input:checked").each(function(){
			tags.push(this.value);
		});
		//alert(tags.length + ' + ' + tags.join(','));
		var tag = tags.join(',');

		var categories = new Array();
		$("#article-category-box").find("input:checked").each(function(){
			categories.push(this.value);
		});

		if(categories.length < 1) {
			alert("You must select a category for this article!");
			return false;
		}
		//alert(categories.length + ' + ' + categories.join(','));
		var category = categories.join(',');

		var articleID = $("#the-article-id").val();
		addMmLightBoxMsg('Wait a moment please, save article ...', 0, 0, 1);
		$.post($(this).attr('action'), {article_id:articleID, article_title:title, article_tag:tag, article_category:category, article_content:content}, function(dt){
			if (dt.result) {
				$("#article-version-value").text(dt.version);
				$("#the-article-id").val(dt.articleID);
				$("#saved-article-view-link").attr("href", '/article/' + dt.articleID + '.html');
				$(".edit-article-info").show();
			}
			addMmLightBoxMsg(dt.msg, 1);
			window.setTimeout(function(){removeMmLightBoxMsg();}, 2000);
		}, 'json');

		return false;
	});
}
